﻿requirejs.config({
	baseUrl : ".",
	paths : {
		zepto:"javascripts/zepto.min",
    	dingtalk:"javascripts/dingtalk",
		vue : "components/vue/vue.min",
		axios : "components/axios/axios.min",
		MINT : "components/mint-ui/index",
		moment : "components/moment/moment.min",
		search : "resources/js/search",
		datetime : "resources/js/datetime",
		dictionary : "resources/js/dictionary",
		common : "resources/js/common",
		wx : "http://res.wx.qq.com/open/js/jweixin-1.2.0",
		jssdk : "resources/js/jssdk"
	},
	shim : {
		"MINT" : {
			deps : [ "vue" ],
			exports : "MINT"
		},
		"wx" : {
			exports : "wx"
		}
	}
});

require([ "zepto","dingtalk","vue", "axios", "common", "MINT", "moment", "search", "datetime", "dictionary", "jssdk" ], function(zepto,dingtalk,Vue, axios, common, MINT, moment, search, datetime, dictionary, jssdk) {
	Vue.component(MINT.Popup.name, MINT.Popup);
	Vue.component(MINT.Toast.name, MINT.Toast);

	window.MINT = MINT;

	var vm = new Vue({
		el : "#view",
		data : {
			detail : {},
			nextAssigner : "",
			suggestion : "",
			incident_solutionway : [],
			users : [],
			bpm_commons : [],
			commons : "",
			menuVisible : false,
			submitVisible : false,
			submitLine : {},
			images : [],
			readonly : false,
			others : {},
			supporter : {
				id : "",
				name : ""
            },
            imagePaths: []
		},
		computed : {
			menuImage : function() {
				return this.menuVisible ? "resources/image/backlogDetail/menuClose.png" : "resources/image/backlogDetail/menuShow.png"
			}
		},
		mounted : function() {
			var self = this;

			var id = common.getParam("id");
			this.readonly = common.getParam("readonly") == 1;

			axios.get("/common/dict?typeCode=INCIDENT_SOLUTIONWAY").then(function(response) {
				self.incident_solutionway = response.data;
			});

			axios.get("/incident/detail?id=" + id).then(function(response) {
				self.detail = response.data;
				self.supporter.id = response.data.supporter_id;
				self.supporter.name = response.data.supporter_name;
            });

            axios.get("/incident/images?id=" + id).then(function (response) {
                for (var i = 0; i < response.data.length; i++) {
                    response.data[i] = common.ddurl + response.data[i];
                }
                self.imagePaths = response.data;
            });
		},
		methods : {
			menuClick : function() {
				this.menuVisible = !this.menuVisible;
			},
			showImg:function(data)
			{
				
			},
			gotoHandleProcess : function() {
				window.location.href = "incidentHandleProcess.html?id=" + this.detail.id + "&processId=" + this.detail.process_id;
			},
			gotoHitory : function() {
				window.location.href = "incidentHistory.html?cus_user_id=" + this.detail.cus_user_id;
			},
			commonsChange : function(event) {
				this.suggestion += event.target.value;
			},
			imgUpload : function(event) {
				common.imgUpload(event, this.images, MINT.Toast);
			},
			submit : function(line) {
				/*
				 * if (common.isEmpty(this.detail.reason_analyze)) {
				 * MINT.Toast("请输入原因分析"); return; } if
				 * (common.isEmpty(this.detail.handle_process)) {
				 * MINT.Toast("请输入处理过程"); return; } if
				 * (common.isEmpty(this.detail.solution)) {
				 * MINT.Toast("请输入解决方案"); return; }
				 */

				if (line.name == "处理完成" && common.isEmpty(this.detail.handle_process)) {
					MINT.Toast("请输入处理过程");
					return;
				}

				if (common.isEmpty(this.supporter.id)) {
					MINT.Toast("请输入支持人员");
					return;
				}

				var self = this;
				try {
					self.others.longitude = jssdk.location().longitude;
					self.others.latitude = jssdk.location().latitude;
				} catch (ex) {
				}

				if (line.name == "转派") {
					if (self.supporter.id == common.getUsername()) {
						alert("请选择其他支持人员");
						return;
					}
				}

				if (line.processMode == 2 || line.commitType != 0 || line.areOpenCommentWindow) {
					this.submitLine = line;
					if (line.processMode == 2 || (line.processMode == 0 && line.commitType == 1)) {
						axios.get("/common/submitUser?key=incidentProcess&processId=" + line.processInstanceId + "&nextTaskId=" + line.nextTaskId + "&businessKey=" + this.detail.id + "&processMode=" + line.processMode).then(function(response) {
							self.users = response.data;
						});
					}

					axios.get("/common/dict?typeCode=BPM_COMMONS").then(function(response) {
						self.bpm_commons = response.data;
					});

					this.submitVisible = true;
				} else {
					var data = {
						data : {
							id : this.detail.id,
							reasonAnalyze : this.detail.reason_analyze,
							handleProcess : this.detail.handle_process,
							solution : this.detail.solution,
							solutionWay : this.detail.solution_way,
							supporterId : self.supporter.id,
							supporterName : self.supporter.name,
							solvedDate : this.detail.solved_date
						},
						line : line,
						images : this.images,
						others : this.others
					};
					common.setColumn(data);

					axios.post("/incident/submit", data).then(function(response) {
						alert(response.data.message);
						common.goback();
					});
				}
			},
			popupSubmit : function() {
				if (this.users && common.isEmpty(this.nextAssigner)) {
					MINT.Toast({
						message : "请输入下一步处理人",
						position : "middle"
					});
					return;
				}

				var self = this;

				var data = {
					data : {
						id : this.detail.id,
						reasonAnalyze : this.detail.reason_analyze,
						handleProcess : this.detail.handle_process,
						solution : this.detail.solution,
						solutionWay : this.detail.solution_way,
						supporterId : self.supporter.id,
						supporterName : self.supporter.name,
						solvedDate : this.detail.solved_date
					},
					nextAssigner : this.nextAssigner,
					suggestion : this.suggestion,
					line : this.submitLine,
					others : this.others
				};
				common.setColumn(data);

				axios.post("/incident/submit", data).then(function(response) {
					alert(response.data.message);
					common.goback();
				});
			},
			cancelSubmit : function() {
				this.submitLine = {};
				this.submitVisible = false;
			}
		}
	});
});